package com.hksj.app.controller;


import com.hksj.app.dto.FileDTO;
import com.hksj.app.service.FileService;
import com.hksj.common.core.model.Rest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "文件管理")
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @ApiOperation(value = "文件上传")
    @PostMapping("/uploadFile")
    public Rest uploadFile(String filePath, MultipartFile[] multipartFile) {
        return Rest.ok(fileService.upload(filePath,multipartFile));
    }

    @ApiOperation(value = "通过文件id查询文件详情")
    @PostMapping("/queryFileDetail")
    public Rest queryFileDetail(@RequestBody FileDTO fileDTO){
        return Rest.ok(fileService.queryFileDetail(fileDTO));
    }

    @ApiOperation(value = "文件下载(统一为zip压缩包)")
    @PostMapping("/downloadFileZip")
    @ResponseBody
    public void downloadFileZip(String id, HttpServletResponse response) {
        fileService.downloadFileZip(id, response);
    }

    @ApiOperation(value = "图片加载")
    @GetMapping("/getFile")
    public void getFile(String id, HttpServletResponse response){
        fileService.getImage(id,response);
    }

    @ApiOperation(value = "文件下载(为上传格式)")
    @PostMapping("/downloadFile")
    @ResponseBody
    public void downloadFile(String id, HttpServletResponse response) {
        fileService.downloadFile(id, response);
    }

    @ApiOperation(value = "网络文件转换成文件流")
    @PostMapping("/toFileStream")
    public void toFileStream(String fileURL, HttpServletResponse response){

        fileService.toFileStream(fileURL,response);
    }
}
